<template>
	<view class="community-container">
		<view class="share-box" v-for="(item ,index) in shareList" :key="index">
			<view class="title-box">{{item.CommunityTitle}}</view>
			<u-read-more showHeight="100" toggle>
				<rich-text :nodes="item.RecommendationText" style="white-space: pre-wrap;"></rich-text>
			</u-read-more>
			<view class="comment-box">
				<view class="content-comment">
					评论：{{item.list.length > 0 ? '' : '暂无'}}
				</view>
				<text v-for="(item1,index1) in item.list" :key="index1">{{item1.OfficeName}}：{{item1.Content}}</text>
			</view>
			<view style="margin-top: 20rpx;">
				<u-input size="" placeholder="请输入评论" v-model="item.Content" border="surround">
					<template slot="suffix">
						<u-button type="success" size="mini" @click="sendComment(item)">发送</u-button>
					</template>
				</u-input>
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shareList: [],
			}
		},
		onShow() {
			this.getShsreList()
		},
		methods: {
			async getShsreList() {
				try {
					const {
						data
					} = await uni.$http.get('/community_records');
					if (data.code === 200) {
						const shareList = await Promise.all(data.records.map(async (item) => {
							const contentList = await this.getCommentList(item.RecommendationID);
							return {
								...item,
								list: contentList
							};
						}));
						this.shareList = shareList;
					}
				} catch (error) {
					console.error('获取分享列表失败:', error);
				}
			},
			async getCommentList(id) {
				try {
					const {
						data
					} = await uni.$http.get('/community_comments', {
						RecommendationID: id
					});
					if (data.code === 200) {
						return data.records;
					}
					return []; 
				} catch (error) {
					console.error('获取评论列表失败:', error);
					return [];
				}
			},
			async sendComment(item) {
				const {
					RecommendationID,
					OfficerID,
					Content
				} = item

				if (!Content) {
					return this.$refs.uToast.show({
						message: '请输入评论！',
						type: 'error'
					});
				}
				const {
					data
				} = await uni.$http.post('/publish_comment', {
					RecommendationID,
					OfficerID,
					Content
				})
				if (data.code === 200) {
					const that = this
					this.$refs.uToast.show({
						message: '评论成功！',
						type: 'success',
						complete() {
							that.getShsreList()
						}
					});
				}
			}
		}

	}
</script>

<style lang="scss" scoped>
	.community-container {
		padding: 20rpx;
		height: 100vh;
		overflow: auto;
		background-color: #f8f8f8;

		.share-box {
			width: 100%;
			padding: 20rpx 30rpx;
			box-sizing: border-box;
			background-color: #fff;
			margin-bottom: 30rpx;
			border-radius: 5rpx;

			.title-box {
				font-size: 30rpx;
				color: #333;
				font-weight: bold;
				margin-bottom: 20rpx;
			}

			.comment-box {
				margin-top: 20rpx;
				display: flex;
				flex-direction: column;
				background-color: #f8f8f8;
				padding: 10rpx;
				box-sizing: border-box;

				.content-comment {
					width: 100%;
					font-size: 26rpx;
					margin: 20rpx 0;
					box-sizing: border-box;
				}

				text {
					font-size: 24rpx;
					margin-bottom: 10rpx;
				}
			}

			::v-deep .u-input__content__field-wrapper__field {
				height: 40rpx;
				font-size: 24rpx !important;
			}
		}
	}
</style>